<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Slideshow;
use think\Request;
use think\File;
class  Slide extends Base
{
    public function lists(){
        $user = $this->user_info();
        $resource = collection(Slideshow::order("sort asc")->select())->toArray();
        $data = array(
            "setting" => $this->getAllSetting(),
            "list"=>$resource,
        );
        return view("lists",$data);

    }
    //上传文件
    public function upload(Request $request){
        //接收上传的文件
        $file = $this->request->file('file');
        if(!empty($file)){
            //图片存的路径
            $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'slide';

            // 移动到框架应用根目录/public/uploads/ 目录下

            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->rule('uniqid')->move($imgUrl);
            $error = $file->getError();
            //验证文件后缀后大小
            if(!empty($error)){
                dump($error);exit;
            }
            if($info){
                // 成功上传后 获取上传信息

                //获取图片的名字
                $imgName = $info->getFilename();
                //获取图片的路径
                $photo= $imgName;

            }else{
                // 上传失败获取错误信息
                $file->getError();
            }
        }else{
            $photo = '';
        }
        if($photo !== ''){
            return ['code'=>1,'msg'=>'成功','photo'=>$photo];
        }else{
            return ['code'=>404,'msg'=>'失败'];
        }
    }
    //添加
    public function addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => Slideshow::select(),
        );
        return view("addform",$data);
    }
    public function add(Request $request){
        $data = $request->post();
        $data['create_time'] = time();
        $data['update_time'] = time();
        $res = Slideshow::insert($data);
        if($res){
            $this->success("添加成功","Slide/lists");
        }else{
            $this->error("添加失败","Slide/addform");
        }
    }
    //修改
    public function update(Request $request){
        $user = $this->user_info();
        $id = $request->get("id");
        $data = array(
            "setting" => $this->getAllSetting(),
            "data" => Slideshow::where(array("id"=>$id))->find(),
        );
        return view("update",$data);
    }
    public function toUpdate(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $data['update_time'] = time();
        if(Slideshow::update($data)){
            $this->success("修改成功","Slide/lists");
        }else{
            $this->error("修改失败");
        }

    }
    //删除
    public function deletes(Request $request){
        $user = $this->user_info();
        if(Slideshow::destroy($request->post('id'))){
            $this->success("删除成功","Slide/lists");
        }else{
            $this->error("删除失败");
        }
    }
    //修改状态
    public function setStatus(Request $request){
        $data = $request->post();
        if(Slideshow::update($data)){
            $this->success("成功");
        }else{
            $this->error("失败");
        }
    }
}
